package com.zenith.zongheng.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zenith.zongheng.common.domain.vo.PageVO;
import com.zenith.zongheng.demo.domain.dto.LeaveStatisticsDTO;
import com.zenith.zongheng.demo.domain.entity.LeaveInfo;
import com.zenith.zongheng.demo.domain.vo.LeaveInfoVO;
import com.zenith.zongheng.logs.annotation.Log;
import com.zenith.zongheng.logs.enums.LogOptTypeEnum;
import com.zenith.zongheng.system.api.SysUnitinfoService;
import com.zenith.zongheng.system.api.SysUnituserService;
import com.zenith.zongheng.system.api.SysUnituserTreeService;
import com.zenith.zongheng.system.api.SysUserinfoService;
import com.zenith.zongheng.system.dao.SysUnitinfoMapper;
import com.zenith.zongheng.system.domain.entity.SysUnitinfo;
import com.zenith.zongheng.system.domain.entity.SysUserinfo;
import com.zenith.zongheng.system.domain.vo.SysSyncUnitTreeVo;
import com.zenith.zongheng.system.domain.vo.SysUnitinfoVO;
import com.zenith.zongheng.system.domain.vo.SysUnituserVO;
import com.zenith.zongheng.system.domain.vo.SysUserinfoVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zenith.zongheng.demo.domain.dto.LeaveInfoDTO;
import com.zenith.zongheng.demo.domain.dto.LeaveInfoSaveDTO;
import com.zenith.zongheng.demo.api.LeaveInfoService;
import com.zenith.zongheng.core.result.R;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tangzx
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/leaveInfo")
public class LeaveInfoController {
    @Autowired
    @Reference
    private LeaveInfoService leaveInfoService;


    @Autowired
    @Reference
    private SysUnitinfoService sysUnitinfoService;

    @Autowired
    @Reference
    private SysUnituserService sysUnituserService;

    @Autowired
    @Reference
    private SysUserinfoService sysUserinfoService;


    @PostMapping("/save")
    public R<Object> createLeaveInfo(@RequestBody LeaveInfoSaveDTO dto) {
        leaveInfoService.saveLeaveInfo(dto);
        return R.ok();
    }

    @PostMapping("/update")
    public R<Object>  updateLeaveInfo(@RequestBody LeaveInfoSaveDTO dto) {
        leaveInfoService.updateLeaveInfo(dto);
        return R.ok();
    }
    @PostMapping("/delete")
    public R<Object> deleteLeaveInfo(@RequestBody Long... ids) {
        leaveInfoService.deleteLeaveInfo(ids);
        return R.ok();
    }

    /**
     * 页面刷新通过id获得指定的请假信息
     * @author: wuyue
     */
    @Log(value = "假条ID", module = "leave", optType = LogOptTypeEnum.OPERATE_QUERY)
    @GetMapping("/getById/{id}")
    public R<LeaveInfo> getById(@PathVariable Long id) {
        return R.ok(leaveInfoService.getById(id));
    }

    @PostMapping("/list")
    public R<PageVO<LeaveInfoVO>> list(@RequestBody(required = false) LeaveInfoDTO dto)
    {
        return R.ok(leaveInfoService.queryPage(dto));
    }

    /**
     * 年内休假情况，查询该用户一年内的请假情况，没有返回0
     * @author wuyue
     */
    @Log(value = "年内请假次数", module = "leave", optType = LogOptTypeEnum.OPERATE_QUERY)
    @PostMapping("/count/year")
    public R<Integer> getLeaveCount(@RequestBody Long unitUserId)
    {
        return R.ok(leaveInfoService.leaveCountByYear(unitUserId));
    }

    /**
     * 统计所有请假信息
     *
     * @author zza
     * @since 2021-02-07
     */
    @PostMapping("/statistics")
    public R<Map<SysUnitinfoVO,List<Map<SysUserinfo, List<LeaveInfo>>>>> getStatistics(@RequestBody LeaveStatisticsDTO dto){
        Map<SysUnitinfoVO, List<Map<SysUserinfo, List<LeaveInfo>>>> statistics = leaveInfoService.statistics(dto);
        return R.ok(statistics);
    }


}

